import { DictValueService } from '@src/service/admin/dictValue/dict-value.service';
import { DictService } from '@src/service/admin/dict/dict.service';
import { Controller, Post, HttpStatus, HttpCode, Body, Param, Delete, Patch, Get, Query, ParseIntPipe } from '@nestjs/common';
import { ApiTags, ApiBearerAuth, ApiOperation, ApiCreatedResponse, ApiOkResponse } from '@nestjs/swagger';
import adminConfig from '@src/config/admin.config';
import { CreateDictDto, DictRep } from './dto';

@ApiTags('api-dict')
@ApiBearerAuth()
@Controller(`${adminConfig.adminPath}/dict`)
export class DictController {
  constructor(
    private readonly dictService: DictService,
    private readonly dictValueService: DictValueService,
  ) { }

  /**
   * @Description: 添加字典
   * @param {type}
   * @return:
   */
  @ApiOperation({ summary: '创建字典值', description: '输入字典值名称' })
  @ApiCreatedResponse({
    type: CreateDictDto,
    description: '创建字典值DTO'
  })
  @ApiOkResponse({ type: DictRep })
  @Post('create')
  @HttpCode(HttpStatus.CREATED)
  async createDict(@Body() createDictDto: CreateDictDto): Promise<DictRep> {
    const { dictList, name, ...other } = createDictDto;
    const unique = await this.dictService.checkUnique(name);
    const dict = await this.dictService.create({ name, ...other });
    const result = await this.dictService.save(dict);
    const { id } = result;
    const assign = await this.dictValueService.assignData({ dictId: id, dictList })

    return result;
  }

  /**
  * @Description: 获取字典列表
  * @param {type}
  * @return:
  */
  @ApiOperation({ summary: '获取字典列表', description: '获取字典列表' })
  @ApiOkResponse({ type: [DictRep] })
  @Get('list')
  @HttpCode(HttpStatus.OK)
  public async dictList(@Query() queryOption: { [propsName: string]: any }): Promise<DictRep[]> {
    return await this.dictService.findPage(queryOption);
  }
}